//
//  ZYNearViewController.m
//  YaYa
//
//  Created by 张亚 on 2019/1/9.
//  Copyright © 2019年 张亚. All rights reserved.
//

#import "ZYNearViewController.h"
#import "ZYNearLiveCell.h"
#import "ZYLiveModel.h"

static NSString * CollectViewCellID = @"NearViewController";

@interface ZYNearViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) NSMutableArray * datalist;

@end

@implementation ZYNearViewController
- (NSMutableArray *)datalist {
    if (_datalist == nil) {
        _datalist = [NSMutableArray array];
    }
    return _datalist;
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"ZYNearLiveCell" bundle:nil] forCellWithReuseIdentifier:CollectViewCellID];

    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
    
    
    //构造数据源
    for (int i = 0; i < 100; i++) {
        NSString * name = @"WechatIMG340";
        NSString * title = [NSString stringWithFormat:@"第%d个",i];
        
        ZYLiveModel * model = [[ZYLiveModel alloc] init];
        model.name = name;
        model.title = title;
        [self.datalist addObject:model];
    }
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datalist.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYNearLiveCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectViewCellID forIndexPath:indexPath];
    cell.live = self.datalist[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

    ZYNearLiveCell * liveCell = (ZYNearLiveCell *)cell;
    liveCell.live = self.datalist[indexPath.row];
    [liveCell showAnimation];
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger count = self.collectionView.width/100;
    CGFloat itemW = (self.collectionView.width - 5 * (count +1)) / count;
    
    return CGSizeMake(itemW, itemW + 20);
    
}



@end
